﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;


namespace SysWaterMark
{
    public partial class Form1 : Form
    {
        //private Label waterMarkLabel;
 
        public Form1()
        {
            //waterMarkLabel = new Label
            //{
            //    Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right),
            //    Font = new Font("Microsoft Sans Serif", 80F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))),
            //    ForeColor = SystemColors.ControlDarkDark,
            //    Location = new Point(126, 178),
            //    Name = "WATERMARK",
            //    Size = new Size(338, 120),
            //    TabIndex = 0,
            //    Text = "HELLO WORLD",
            //    TextAlign = ContentAlignment.MiddleCenter
            //};

            InitializeComponent();
            SuspendLayout();
            AutoScaleDimensions = new SizeF(6F, 13F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(579, 489);
            ControlBox = false;
            FormBorderStyle = FormBorderStyle.None;
            MaximizeBox = false;
            MinimizeBox = false;
            Opacity = 0.02D;
            ShowIcon = false;
            ShowInTaskbar = false;
            TopMost = true;
            var hwnd = Handle;
            WindowsServices.SetWindowExTransparent(hwnd);
            TopMost = true;
            AllowTransparency = true;
            ResumeLayout(false);
            //Controls.Add(waterMarkLabel);
            WindowState = FormWindowState.Maximized;
            base.SetVisibleCore(true);
        }

        protected override CreateParams CreateParams
        {
            get
            {
                const int WS_EX_APPWINDOW = 0x40000;
                const int WS_EX_TOOLWINDOW = 0x80;
                CreateParams cp = base.CreateParams;
                cp.ExStyle &= (~WS_EX_APPWINDOW);    // 不显示在TaskBar
                cp.ExStyle |= WS_EX_TOOLWINDOW;      // 不显示在Alt+Tab
                return cp;
            }
        }
        
        protected override void OnPaint(PaintEventArgs e)
        {

            base.OnPaint(e);
            Graphics g = e.Graphics;
            Font myFont = new Font("黑体", 45, FontStyle.Bold );
            float angle = -45; //角度
            float l = 0;
            int spacing = 300; 
            Random rnd = new Random();

            g.Clear(this.BackColor);

            GraphicsText graphicsText = new GraphicsText();
            graphicsText.Graphics = e.Graphics;

            // 绘制围绕点旋转的文本  
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;

            string str = "10.180.13.44";
            SizeF size = g.MeasureString(str, myFont);

            int heightNum = this.Height / spacing +1;

            int rowItemNum = (this.Width / (spacing))+2;

            for (int i = 0; i < heightNum; i++)
            {
                for (int j = 0; j < rowItemNum; j++) {

                    graphicsText.DrawString(str, myFont, RndBrush(this.BackColor, rnd), new PointF(j * spacing  , (i * spacing)) , format, -45f);

                }
                
            }

        }
 

        private SolidBrush RndBrush(Color BackColor, Random rnd)
        {

            return new SolidBrush(Color.Black);

            int r, g, b;

            do
            {
                r = rnd.Next(255);
            } while (Math.Abs(r - BackColor.R) < 50); //这是为了控制颜色不要和背景色太接近
            do
            {
                g = rnd.Next(255);
            } while (Math.Abs(g - BackColor.G) < 50);
            do
            {
                b = rnd.Next(255);
            } while (Math.Abs(b - BackColor.B) < 50);

            return new SolidBrush(Color.FromArgb(255, r, g, b));

        }

        //private void pictureBox1_Paint(object sender, PaintEventArgs e)
        //{
        //    base.OnPaint(e);
        //    Graphics g = e.Graphics;
        //    char[] code = "测试文字".ToCharArray(); //文字
        //    Font myFont = new Font("宋体", 20);
        //    float angle = 60; //角度
        //    float l = 0;
        //    Random rnd = new Random();
        //    g.Clear(this.BackColor);
        //    for (int i = 0; i < code.Length; i++)
        //    {
        //        string s = new string(code[i], 1);
        //        g.ResetTransform();
        //        SizeF size = g.MeasureString(s, myFont);
        //        g.TranslateTransform(l + size.Width / 2, size.Height / 2); //设置旋转中心为文字中心
        //        g.RotateTransform((float)(rnd.NextDouble() * angle * 2 - angle)); //旋转
        //        g.DrawString(s, myFont, RndBrush(this.BackColor, rnd), new PointF(-size.Width / 2, -size.Height / 2));
        //        l += size.Width;
        //    }
        //}
    }

    public static class WindowsServices
    {
        const int WS_EX_TRANSPARENT = 0x00000020;
        const int GWL_EXSTYLE = (-20);

        [DllImport("user32.dll")]
        static extern int GetWindowLong(IntPtr hwnd, int index);

        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);

        public static void SetWindowExTransparent(IntPtr hwnd)
        {
            var extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
            SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
        }
    }
}
